package cn.jwt.handler;

import cn.jwt.domain.ResponseResult;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;


/*作为spring的异常处理器*/
//@ControllerAdvice
@Slf4j
@RestControllerAdvice
public class ProjectExceptionAdvice{

    /**
     * 入参格式错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageConversionException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ResponseResult wrongFormat(Exception e) {
        log.error(e.getLocalizedMessage(), e);
        return ResponseResult.fail("入参格式错误");
    }

    /**
     * 空指针异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult NullPointerException(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("空指针异常");
    }

    /**
     * 类型强制转换异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ClassCastException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult ClassCastException(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("类型强制转换异常");
    }

    /**
     * 操作数据库异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(SQLException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult SQLException(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("操作数据库异常");
    }

    /**
     * 断言异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(AssertionError.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult AssertionError(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("断言异常");
    }

    /**
     * 不支持的类版本错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(UnsupportedClassVersionError.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult UnsupportedClassVersionError(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("不支持的类版本错误");
    }

    /**
     * 验证错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(VerifyError.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult VerifyError(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("验证错误");
    }

    /**
     * 找不到类异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ClassNotFoundException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult ClassNotFoundException(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("找不到类异常");
    }


    /**
     * 违法的访问异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(IllegalAccessException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult IllegalAccessException(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("违法的访问异常");
    }

    /**
     * 实例化异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(InstantiationException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult InstantiationException(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("实例化异常");
    }

    /**
     * token非法
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ExpiredJwtException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult ExpiredJwtException(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("token非法");
    }


    /**
     * 被中止异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(InterruptedException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult InterruptedException(Exception e) {
        log.error(e.getLocalizedMessage());
        return ResponseResult.fail("被中止异常");
    }

    /*@ExceptionHandler(value = Exception.class)
    public ResponseResult handler(Exception ex) {
        log.info("全局异常捕获:{}",ex);
        return ResponseResult.fail("服务器故障请联系管理员");
    }*/

}
